mod config;
mod init;
mod udp;
mod var;
use futures::join;
use rmw_upnp::daemon;
use std::thread;

#[async_std::main]
async fn main() {
  init::init();
  let port = config::get("udp/port", || {
    let socket = std::net::UdpSocket::bind("0.0.0.0:0").unwrap();
    socket.local_addr().unwrap().port()
  });

  let duration = 60;
  let recver = daemon("rmw upnp test".to_string(), port, duration);
  thread::spawn(move || loop {
    if let Ok(r) = recver.recv() {
      match r {
        Ok((ex_port, ip, gateway)) => {
          println!("{} -> {}:{} gateway {:?}", ex_port, ip, port, gateway);
        }
        Err(err) => println!("{:?}", err),
      }
    }
  });

  let _ = join!(udp::init(port));
}
